package com.baizhi.controller;

import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Category;
import com.baizhi.service.CategoryService;
import com.baizhi.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;


@RestController  //将controller组件对象交给工厂管理
@CrossOrigin  //跨域： 当一个请求url的协议、域名、端口三者之间任意一个与当前页面url不同即为跨域
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("queryOnePage")
    @ResponseBody
    public CategoryVO queryOnePage(@RequestBody PageDTO pageDTO){
        return categoryService.queryOnePage(pageDTO.getPage(),pageDTO.getPageSize());
    }

    @RequestMapping("add")
    @ResponseBody
    public Map<String, String> add(@RequestBody Category category){
        return categoryService.add(category);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String, String> delete(@RequestBody Category category){
        return categoryService.delete(category.getId(),category.getParentId());
    }

    @RequestMapping("queryById")
    @ResponseBody
    public Category queryById(int id){
        return categoryService.queryById(id);
    }

    @RequestMapping("update")
    @ResponseBody
    public Map<String, String> update(@RequestBody Category category){
        return categoryService.update(category);
    }

    @RequestMapping("queryTwoPage")
    @ResponseBody
    public CategoryVO queryTwoPage(@RequestBody PageDTO pageDTO){
        return categoryService.queryTwoPage(pageDTO.getPage(), pageDTO.getPageSize(), pageDTO.getCategoryId());
    }


    //根据级别查询类别
    @RequestMapping("queryByLevelsCategory")
    @ResponseBody
    public List<Category> queryByLevelsCategory(Integer levels){
        return categoryService.queryByLevelsCategory(levels);
    }

}
